require('../../../../less/back/rent/rent_detail.less');
import $ from 'jQuery';
(function () {
    let orderData = JSON.parse(IOT.getSessionStore(URI.RENT.RETURN.DETAIL.PAGE));
    let flag =1;
    let orderInfo = [];//原始订单的详情的剩余数量
    let $vue = null;
    $vue = new Vue({
        el: '.client-detail-hook',
        data: {
            Isshow:true,
            distributionMode:[{id:1,name:'租方自取'},{id:2,name:'承租方配送'}],
            info: {
                totalQuantity:'0',
                distributionMode:'',
                pickupAddress1:'',
                pickupAddress:''
            },
            data:{
                pickupAddress1:''
            }
        },
        created: function () {
            if(orderData.orderStatus!="3"){
                $('#print_btn').show();//打印
                $('#export_btn').show();//导出
            }
            if( orderData.orderStatus =="0" &&  M.Authority.checkAuthority('unsubscribe-edit')){
                $('#edit_btn').show();//修改
            }
            if( orderData.orderStatus =="0" &&  M.Authority.checkAuthority('unsubscribe-confirm')){
                $('#audit_btn').show();//审核
            }
            if( orderData.orderStatus=="1" &&  M.Authority.checkAuthority('unsubscribe-sure-order')){
                $('#confirm_btn').show();//确认收板
            }
            if((orderData.orderStatus == '0' ||orderData.orderStatus == '1' ) && M.Authority.checkAuthority('unsubscribe-discard')){
                $('#scrap_btn').show();//废弃
            }else if( orderData.orderStatus == '3' && M.Authority.checkAuthority('unsubscribe-recover') ){
                $('#unScrap_btn').show();//恢复
            }
            /*查询原订单的剩余数*/
            IOT.getServerData(URI.RENT.RENTAL.DETAIL.DATA, {orderNo: orderData.fromOrderNo}, (ret) => {
                if (ret.code === 200) {
                    orderInfo =  ret.data;
                    if((orderData.orderStatus == '1' ||orderData.orderStatus == '2' ) && orderData.isOneClick =="N" && orderInfo.surplus > 0 && M.Authority.checkAuthority('unsubscribe-one-key') ){
                        $('#aSword_btn').show();//一键下单
                    }
                } else {
                    IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                }
            });
            //获取详情接口数据
            IOT.getServerData(URI.RENT.RETURN.DETAIL.DATA, {orderNo: orderData.orderNo}, (ret) => {
                if (ret.code === 200) {
                    this.info = $.extend({}, this.info, ret.data);
                    this.data = $.extend({}, this.data, ret.data);
                    $vue.$data.info.pickupAddress1 = $vue.$data.info.pickupAddress;
                    $vue.$data.data.pickupAddress1 = $vue.$data.data.pickupAddress;
                    //退板日期
                    if($vue.$data.info.confirmTime == null){
                        $vue.$data.info.confirmTime = '-';
                    }else{
                        $vue.$data.info.confirmTime = new Date($vue.$data.info.confirmTime).Format('yyyy-MM-dd hh:mm:ss');
                    }
                    //配送方式
                    if($vue.$data.info.distributionMode == 1){
                        $vue.$data.info.distributionMode = "租方自取"
                    }else {
                        $vue.$data.info.distributionMode = "承租方配送"
                    }
                } else {
                    IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                }
            });
        },
        computed:{
            totalCosts:function(){ //实际退费总计
                return  this.info.returnActual;
            },
            totalRental:function(){//退租金
                return this.info.returnRental;
            },
            totalDeposit:function(){//退保证金
                return this.info.returnDeposit;
            }
        },
        methods: {
            cancleBack: function () {
                M.Page.emitPrePage();
            },
            changeVal:function(){
                flag =0;
                var regu = /^[1-9]\d*$/;
                if(!regu.test($vue.$data.info.palletTypesNum)){
                    layer.alert('托盘租赁数量必须为正整数');
                    return false;
                }
                var max = orderInfo.surplus + $vue.$data.data.palletTypesNum;
                if($vue.$data.info.palletTypesNum > max){
                    layer.alert('托盘退板总数超过原订单托盘数，请重新输入');
                    return false;
                }
            },
            //费用预估
            priceReview:function(){
                flag =1;
                var regu = /^[1-9]\d*$/;
                if(!regu.test($vue.$data.info.palletTypesNum)){
                    layer.alert('托盘租赁数量必须为正整数');
                    return false;
                }
                var max = orderInfo.surplus + $vue.$data.data.palletTypesNum;
                if($vue.$data.info.palletTypesNum > max){
                    layer.alert('托盘退板总数超过原订单托盘数，请重新输入');
                    return false;
                }
                let params ={
                    totalQuantity :$vue.$data.info.palletTypesNum,
                    customerId:orderData.customerId,
                    orderType:'4',
                    orderNo:orderData.fromOrderNo
                };
                IOT.getServerData(URI.RENT.RETURN.DETAIL.GET_COST, params, (ret) => {
                    if (ret && ret.code === 200) {
                        this.$nextTick(() => {
                            $vue.$data.info.returnRental =ret.data.totalRental;
                            $vue.$data.info.returnDeposit =ret.data.totalDeposit;
                            $vue.$data.info.returnActual =ret.data.totalCosts;
                        })
                    } else {
                        IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                    }
                });
            },
            editOrder:function(){//修改订单
                $vue.$data.Isshow = false;
                $vue.$data.info.pickupAddress1 = $vue.$data.info.pickupAddress;
                $('.detail_list>li>p input').removeClass('no_border').prop('readonly',false);
                //百度地图地址逆解析
                var map = new BMap.Map('return-map');
                map.centerAndZoom(new BMap.Point(116.404, 39.915), 5);
                //输入地址事件处理 start
                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {"input" :"pickupAddress"
                        ,"location" : map
                    });
                $('#pickupAddress').keyup(function(){
                    $vue.$data.info.pickupAddress = "";
                });
                ac.setInputValue($vue.$data.info.pickupAddress1);
                ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    $("#pickupAddress_list").innerHTML = str;
                });
                var myValue;
                ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: function () {
                            var province = local.getResults().province;
                            var city = local.getResults().city;
                            if(myValue.indexOf(city)==-1){
                                myValue = city+myValue;
                            }
                            if(myValue.indexOf(province)==-1){
                                myValue = province+myValue;
                            }
                            $vue.$data.info.pickupAddress1 = myValue;
                            $vue.$data.info.pickupAddress = myValue;
                            $("#pickupAddress_list").innerHTML =""
                        }
                    });
                    local.search(myValue);
                    $("#pickupAddress_list").innerHTML =""
                });
            },
            saveEdit:function(){
                var regu = /^[1-9]\d*$/;
                if(!regu.test($vue.$data.info.palletTypesNum)){
                    layer.alert('托盘租赁数量必须为正整数');
                    return false;
                }
                var max = orderInfo.surplus + $vue.$data.data.palletTypesNum;
                if($vue.$data.info.palletTypesNum > max){
                    layer.alert('托盘退板总数超过原订单托盘数，请重新输入');
                    return false;
                }
                if(flag ==0){
                    layer.alert('请将需要退板的托盘数进行价格预估！');
                    return false;
                }
                /*if($('#returnAddress')[0].value == ""){
                    layer.alert('请输入收货地址');
                    return false;
                }*/
                if($vue.$data.info.pickupAddress == ""){
                    layer.alert('请按输入地址的搜索列表结果选择收货地址');
                    return false;
                }
                let params ={
                    orderNo:$vue.$data.info.orderNo,
                    totalRental:$vue.$data.info.returnRental,
                    totalDeposit:$vue.$data.info.returnDeposit,
                    totalCosts:$vue.$data.info.returnActual,
                    returnActual:$vue.$data.info.returnActual,
                    distributionMode:$vue.$data.info.distributionMode,
                    totalQuantity:$vue.$data.info.palletTypesNum,
                    palletTypesNum:$vue.$data.info.palletTypesNum,
                    returnAddress:$vue.$data.info.pickupAddress,
                };
                if($vue.$data.info.distributionMode == '租方自取'){
                    params.distributionMode =1;
                }else if($vue.$data.info.distributionMode == '承租方配送'){
                    params.distributionMode =2;
                }
                IOT.showOverlay('保存中，请稍等...');
                IOT.getServerData(URI.RENT.RETURN.DETAIL.MODIFY, params, (ret) => {
                    IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                    IOT.hideOverlay();
                    if (ret && ret.code === 200) {
                        IOT.tips('保存成功！', 'success');
                        layer.closeAll();
                        M.Table.refresh.all();
                        M.Page.emitPrePage();
                    } else {
                        IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                    }
                });
                $vue.$data.Isshow = true;
                $('.detail_list>li>p input').addClass('no_border').prop('readonly',true);
            },
            noEdit:function(){//修改订单——取消按钮，重新赋值
                $vue.$data.Isshow = true;
                $('.detail_list>li>p input').addClass('no_border').prop('readonly',true);
                $vue.$data.info.returnRental = $vue.$data.data.returnRental;
                $vue.$data.info.returnDeposit = $vue.$data.data.returnDeposit;
                $vue.$data.info.returnActual = $vue.$data.data.returnActual;
                $vue.$data.info.palletTypesNum=$vue.$data.data.palletTypesNum;
                $vue.$data.info.distributionMode= $vue.$data.data.distributionMode;
                $vue.$data.info.pickupAddress1=$vue.$data.data.pickupAddress;
                $vue.$data.info.pickupAddress=$vue.$data.data.pickupAddress;
                hideAcBox();
                if($vue.$data.info.distributionMode == '1'){
                    $vue.$data.info.distributionMode="租方自取";
                }else{
                    $vue.$data.info.distributionMode="承租方配送";
                }
            },
            returnConfirm:function() {//确认收货
                layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认退板？', {
                        btn: ['确定','取消']
                }, function(){
                    IOT.showOverlay('保存中，请稍等...');
                    IOT.getServerData(URI.RENT.RETURN.DETAIL.SAVE, {orderNo: orderData.orderNo}, (ret) => {
                        if (ret.code === 200) {
                            IOT.tips('退板成功！', 'success');
                            layer.closeAll();
                            M.Table.refresh.all();
                            M.Page.emitPrePage();
                        } else {
                            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                        }
                    });
                }, function(){
                    //取消
                });
            },
            //导出
            exportOrder:function(){
                IOT.showOverlay('正在导出，请稍等...');
                IOT.getServerData(URI.RENT.RETURN.DETAIL.EXPORT,{orderNo: orderData.orderNo}, (ret) => {
                    IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                    IOT.hideOverlay();
                    if (ret && ret.code === 200) {
                        window.location.href =ret.data.substr(1);
                        IOT.tips('导出完成！', 'success');
                    } else {
                        IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                    }
                });
                return false;
            },
            //确认收货
            confirmOrder:function() {
                layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认收货？', {
                    btn: ['确定','取消']
                }, function(){
                    IOT.showOverlay('保存中，请稍等...');
                    IOT.getServerData(URI.RENT.RETURN.DETAIL.CONFIRM, {orderNo: orderData.orderNo}, (ret) => {
                        IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                        IOT.hideOverlay();
                        if (ret && ret.code === 200) {
                            IOT.tips('确认收货成功！', 'success');
                            layer.closeAll();
                            M.Table.refresh.all();
                            M.refeshMsg();
                            M.Page.emitPrePage();
                        } else {
                            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                        }
                    });
                }, function(){
                    //取消
                });
            },
            auditOrder:function() {//审核
                layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认审核此订单？', {
                        btn: ['确定','取消']
                }, function(){
                    IOT.showOverlay('保存中，请稍等...');
                    IOT.getServerData(URI.RENT.RETURN.DETAIL.AUDIT, {orderNo: orderData.orderNo}, (ret) => {
                        IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                        IOT.hideOverlay();
                        if (ret && ret.code === 200) {
                            IOT.tips('审核成功！', 'success');
                            layer.closeAll();
                            M.Table.refresh.all();
                            M.refeshMsg();
                            M.Page.emitPrePage();
                        } else {
                            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                        }
                    });
                }, function(){
                    //取消
                });
            },
            returnscrapOrder:function() {//废弃
                layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认废弃此订单？', {
                        btn: ['确定','取消']
                }, function(){
                    IOT.showOverlay('保存中，请稍等...');
                    IOT.getServerData(URI.RENT.RETURN.DETAIL.DISCARD, {orderNo: orderData.orderNo}, (ret) => {
                        if (ret.code === 200) {
                            IOT.tips('废弃成功！', 'success');
                            layer.closeAll();
                            M.Table.refresh.all();
                            M.Page.emitPrePage();
                        } else {
                            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                        }
                    });
                }, function(){
                    //取消
                });
            },
            returnunScrapOrder:function() {//恢复
                layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认恢复此订单？', {
                        btn: ['确定','取消']
                }, function(){
                    IOT.showOverlay('保存中，请稍等...');
                    IOT.getServerData(URI.RENT.RETURN.DETAIL.RECOVERY, {orderNo: orderData.orderNo}, (ret) => {
                        if (ret.code === 200) {
                            IOT.tips('恢复成功！', 'success');
                            layer.closeAll();
                            M.Table.refresh.all();
                            M.Page.emitPrePage();
                        } else {
                            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                        }
                    });
                }, function(){
                    //取消
                });
            },
            returnaSword:function(e) {//一键下单
                orderData.aSwordType = e;
                layer.confirm(' &nbsp;&nbsp; &nbsp;&nbsp;是否确认一键下单？', {
                    btn: ['确定','取消']
                }, function(){
                    IOT.showOverlay('保存中，请稍等...');
                    IOT.getServerData(URI.RENT.RETURN.DETAIL.OneKey, {orderNo: orderData.orderNo,aSwordType:orderData.aSwordType}, (ret) => {
                        if (ret.code === 200) {
                            IOT.tips('下单成功！', 'success');
                            layer.closeAll();
                            M.Table.refresh.all();
                            M.Page.emitPrePage();
                        } else {
                            IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                        }
                    });
                }, function(){
                    //取消
                });
            }
        },
        mounted: function () {
            
        }
    });
})();

/*一键下单按钮——悬停下拉*/
$('.btn-group').mouseover(function() {
    $(this).addClass('open');
}).mouseout(function() {
    $(this).removeClass('open');
});

function hideAcBox() {
    var elm = Array.prototype.slice.call(document.getElementsByClassName('tangram-suggestion-main'));
    elm.forEach(function (v, i) {
        v.style.zIndex = -1;
        v.style.visibility = 'hidden';
    });
}